package com.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fire.exception.ServiceException;
import com.fire.model.entity.Notice;
import com.fire.mapper.NoticeMapper;
import com.fire.model.enums.NoticeEnum;
import com.fire.response.ResponseCodeEnum;
import com.fire.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-05
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Override
    public Map<String,Object> queryUnReadNotice() {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("status", NoticeEnum.UNREAD.getCode());
        wrapper.orderByDesc("create_time");
        List<Notice> notices = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(notices)){
            Map<String,Object> map = new HashMap<>();
            map.put("data", notices);
            map.put("count", notices.size());
            return map;
        }
        return null;
    }

    @Override
    public void updateOne(Integer id) throws ServiceException {
        Notice notice = baseMapper.selectById(id);
        if(null == notice){
            throw new ServiceException(ResponseCodeEnum.NOTICE_NOT_FOUND);
        }
        notice.setStatus(NoticeEnum.READ);
        baseMapper.updateById(notice);
    }

    @Override
    public void updateAll() {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("status",NoticeEnum.UNREAD.getCode());
        List<Notice> notices = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty( notices )) {
            notices.stream().forEach(notice ->{
                notice.setStatus(NoticeEnum.READ);
                baseMapper.updateById(notice);
            });
        }
    }

    @Override
    public List<Notice> queryReadNotice() {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("status",NoticeEnum.READ.getCode());
        wrapper.orderByDesc("create_time");
        List<Notice> notices = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty( notices )){
            return notices;
        }
        return null;
    }

    @Override
    public void updateReadOrRuread(Integer id) throws ServiceException {
        Notice notice = baseMapper.selectById(id);
        if(null == notice){
            throw new ServiceException(ResponseCodeEnum.NOTICE_NOT_FOUND);
        }
        if(notice.getStatus().getCode().intValue() == NoticeEnum.READ.getCode()) {
            notice.setStatus(NoticeEnum.UNREAD);
        }else{
            notice.setStatus(NoticeEnum.READ);
        }
        baseMapper.updateById(notice);
    }

    @Override
    public List<Notice> queryAll() {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return baseMapper.selectList(wrapper);
    }
}
